\documentclass[10pt,a4paper]{article} 

\usepackage{ctex} % 中文支持
\usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry} % 页边距
\usepackage{amsmath, amssymb} % 数学公式与符号
\usepackage{graphicx}
\usepackage{pythonhighlight}
\usepackage{url} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{titling}
\setlength{\droptitle}{-2cm} % 标题上移

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%文档的题目、作者与日期
\author{五六七 }
\title{购房意向与逻辑回归 }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\maketitle

\begin{abstract}
在签订初步购房意向书的客户人群中，有多少比例最后确实会购房？
\end{abstract}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\setcounter{tocdepth}{2}
%\renewcommand\contentsname{目录}
%
%\renewcommand {\baselinestretch} {1.3}\normalsize 
%\tableofcontents 
%\renewcommand {\baselinestretch} {1.0}\normalsize


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{问题描述}
在一次住房展销会上，有313名顾客与房地产商签订了初步购房意向书。
在随后的三个月中，其中的一部分最终购买的住房。购买的顾客记为1，没有购买的顾客记为0. 
记自变量 $x$ 为家庭年收入，按照高低不同分成9组，
第 $i$ 组的顾客中，签订购房意向书的人数为 $n_i$, 实际购房的人数为 $m_i$. 实际购房比例为 $p_i$. 

\begin{table}[ht!]\centering
\caption{家庭年收入与购房比例数据} \vspace{0.2cm}
\begin{tabular}{|c|c|c|c|c|}\hline
序号 & 家庭年收入 $x$ 万元 & 签订意向书的人数 $n_i$ & 实际购房的人数 $m_i$ & 购房人数比例 $p_i$ \\ \hline 
1&1.5 &25 &8 & 0.32  \\ \hline 
2&2.5 &32 &13 & 0.4063 \\ \hline 
3&3.5 &58 &26 & 0.4483 \\ \hline 
4&4.5 &52 &22 & 0.4231 \\ \hline 
5&5.5 &43 &20 & 0.4651 \\ \hline 
6&6.5 &39 &22 &0.5641 \\ \hline 
7&7.5 &28 &16 & 0.5714 \\ \hline 
8&8.5 &21 &12 &0.5714 \\ \hline 
9&9.5 &15 &10 &0.6667  \\ \hline 
\end{tabular}
\end{table}

建立最终购买住房的顾客比例与家庭年收入的函数关系式。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{建立模型}

自变量 $x$ 是家庭年收入，这是一个取正实数的量。因变量 $p$ 是购房比例，这是一个在0-1范围内取值的量。
因此可以使用 Logistic 函数
\begin{eqnarray}
p = \frac{\exp(p^*)}{1+\exp(p^*)}
\end{eqnarray}
来连接购房比例与家庭年收入的线性回归模型。
一个自变量的 Logistic 回归模型可以写成 
\begin{eqnarray}
p= \frac{\exp(\beta_0 + \beta_1 x)}{1+\exp(\beta_0 + \beta_1 x)} = \frac{1}{1+\exp(-\beta_0 - \beta_1 x)},  
\end{eqnarray}
其中 $\beta_0, \beta_1$ 是模型参数。这个函数关系式也可以写成
\begin{eqnarray}
\ln \frac{p}{1-p} = \beta_0 + \beta_1 x,  
\end{eqnarray}

将购房比例 $p$ 用连接函数 
\begin{eqnarray}
p^* = \ln \frac{p}{1-p}
\end{eqnarray}
进行变换，得到需要使用的数据为表2. 

\newpage

\begin{table}[ht!]\centering
\caption{线性回归的自变量和因变量数据} \vspace{0.2cm}
\begin{tabular}{|c|c|c|c|c|}\hline
序号 & 自变量 $x$ & 因变量 $p_i^*$ \\ \hline 
1&1.5  & $-0.7538$  \\ \hline 
2&2.5  & $-0.3795$  \\ \hline 
3&3.5  & $-0.2076$  \\ \hline 
4&4.5  & $-0.3102$  \\ \hline 
5&5.5  & $-0.1398$  \\ \hline 
6&6.5  & $0.2578$  \\ \hline 
7&7.5  & $0.2877$  \\ \hline 
8&8.5  & $0.2877$  \\ \hline 
9&9.5  & $0.6931$  \\ \hline 
\end{tabular}
\end{table}

将 $p^*$ 作为因变量，以家庭年收入 $x$ 为自变量，进行一元线性回归。

 \begin{figure}[ht!] \centering
 \includegraphics[height=4cm, width=9cm]{logistic_function.png}
\caption{Logistic 函数}
 \end{figure}
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{编程计算}

\subsection{使用Statsmodels 的最小二乘函数 OLS}

载入数值计算 Numpy包和统计模型 Statsmodels包。
\begin{python}
import numpy as np
import statsmodels.api as sm
\end{python}

从文本文件载入数据。整理成设计矩阵 X, 并计算得到因变量数据 y. 
\begin{python}
a = np.loadtxt('data10_7_1.txt')
x = a[:,0]
pstar = a[:,2]/a[:,1]
X = sm.add_constant(x)
y = np.log(pstar/(1-pstar))
\end{python}

使用最小二乘法构建并拟合模型， 输出模型结果。
\begin{python}
md = sm.OLS(y, X).fit()
print(md.summary())
\end{python}

输出模型结果如下。由此可以得到模型的参数估计为 
%\begin{eqnarray}
$\hat{\beta}_0 = -0.8863,  \hat{\beta}_1 = 0.1558. 
$%\end{eqnarray}

\begin{python}
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.924
Model:                            OLS   Adj. R-squared:                  0.913
Method:                 Least Squares   F-statistic:                     85.42
Date:                Sun, 22 Oct 2023   Prob (F-statistic):           3.59e-05
Time:                        15:46:13   Log-Likelihood:                 6.6829
No. Observations:                   9   AIC:                            -9.366
Df Residuals:                       7   BIC:                            -8.971
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         -0.8863      0.102     -8.653      0.000      -1.128      -0.644
x1             0.1558      0.017      9.242      0.000       0.116       0.196
==============================================================================
Omnibus:                        5.866   Durbin-Watson:                   2.311
Prob(Omnibus):                  0.053   Jarque-Bera (JB):                1.209
Skew:                          -0.036   Prob(JB):                        0.546
Kurtosis:                       1.206   Cond. No.                         14.6
==============================================================================
\end{python}

预测当家庭年收入为9万元的客户的购房比例。
\begin{python}
p0=1/(1+np.exp(-md.predict([1,9])))
print("所求比例p0=%.4f"%p0)
\end{python}

从拟合好的回归模型里提取模型参数。也可以直接看模型输出结果。
\begin{python}
b = md.params  
\end{python}

\subsection{使用Statsmodels 的广义线性模型函数 glm}

同样地，先载入数值计算 Numpy包和统计模型 Statsmodels包。
\begin{python}
import numpy as np
import statsmodels.api as sm
\end{python}

再载入数据，从中提取第0列为自变量数据 x, 第2列为最终购房人数，第1列减去第2列为有购房意向但未购房的人数。 
即变量 y 保存的第1列为成功的次数，第2列为失败次数。
\begin{python}
a = np.loadtxt('data10_7_1.txt')
x = a[:,0]
y = np.vstack([a[:,2], a[:,1]-a[:,2]]).T
\end{python}

将自变量数据和因变量数据保存为一个字典型变量。
\begin{python}
d = {'x':x, 'y':y}  
\end{python}

使用广义线性回归的 glm 函数，建立并拟合一个 Logistic 模型。
\begin{python}
md = sm.formula.glm('y~x',d, family=sm.families.Binomial()).fit()
print(md.summary())
\end{python}

输出的模型结果如下。注意到参数的估计值稍有不同。
\begin{python}
Generalized Linear Model Regression Results                  
==============================================================================
Dep. Variable:       ['y[0]', 'y[1]']   No. Observations:                    9
Model:                            GLM   Df Residuals:                        7
Model Family:                Binomial   Df Model:                            1
Link Function:                  Logit   Scale:                          1.0000
Method:                          IRLS   Log-Likelihood:                -18.046
Date:                Sun, 22 Oct 2023   Deviance:                       1.0467
Time:                        16:07:43   Pearson chi2:                     1.05
No. Iterations:                     3   Pseudo R-squ. (CS):             0.5931
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept     -0.8518      0.293     -2.906      0.004      -1.426      -0.277
x              0.1498      0.053      2.805      0.005       0.045       0.254
==============================================================================
\end{python}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{检验模型}

查看回归模型的 $R^2$ 为 0.924. 说明模型拟合较好。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{ROC与AUC介绍}

考虑一个二分类任务的模型，分类结果为 positive 和 negative. 
根据使用模型得到的分类正确与错误，可以计算混淆矩阵如表3。
\begin{table}[ht!]\centering
\caption{Confusion Matrix} \vspace{0.2cm}
\begin{tabular}{|c|c|c|}\hline
   &Positive & Negative  \\ \hline 
Predict Yes &True Positive & False Positive \\ \hline 
Predict No & False Negative & True Negative  \\ \hline 
\end{tabular}
\end{table}

定义假阳率为所有阴性对象中，模型分类为阳性的比例。定义真阳率为所有阳性对象中，模型分类为阳性的比例。
\begin{eqnarray}
false\,\, positive\,\, rate &=& \frac{False\, Positive}{Negative} \\ 
true\,\, positive\,\, rate &=& \frac{True\, Positive}{Positive} %= recall \\ 
%precision &=& \frac{True\, Positive}{True\, Positive + False\, Negative} \\ 
%accuracy &=& \frac{True\, Positive + True\, Negative}{Positive + Negative} \\ 
%F-measure &=& \frac{2}{1/precision + 1/recall}
\end{eqnarray}

参考文献 \cite{tm-fawcett} 中的介绍，设一个分类模型对20个目标对象的得分如图2。

\begin{figure}[ht!] \centering
\includegraphics[height=7cm, width=10cm]{ROC_curve_instances.png}
\caption{一个分类模型的得分}
\end{figure}

取一些不同的阈值，可以画出相应的ROC曲线如图3。

\newpage

\begin{figure}[ht!] \centering
\includegraphics[height=8cm, width=12cm]{ROC_curve_with_thresholds.png}
\caption{ROC曲线上的小数代表不同的阈值}
\end{figure}

 AUC 值就是 ROC曲线下方的面积。这是一个介于0到1之间的数。这个AUC数值越接近1，说明模型的分类效果越好。 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{回答问题}

数据变换后的线性回归方程为 
\begin{eqnarray}
\hat{p^*} =  -0.8863 + 0.1558x,  
\end{eqnarray}

原来的 Logistic 回归方程为 
\begin{eqnarray}
\hat{p} = \frac{1}{1+\exp(0.8863 - 0.1558x)}.   
\end{eqnarray}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\section{参考文献 }
\begin{thebibliography}{99}

%\bibitem{dingtongren} 丁同仁、李承治，常微分方程教程，高等教育出版社，2022年3月第三版。
\bibitem{sishoukui-2} 司守奎,孙玺菁. \emph{Python数学建模算法与应用}, 国防工业出版社. 2022年1月第1版. 
\bibitem{hexiaoqun-ara} 何晓群. \emph{应用回归分析(R语言版)}. 电子工业出版社. 2017年7月第1版. 
\bibitem{dalgaard} Peter Dalgaard 著, 郝智恒等译. \emph{R语言统计入门}. 人民邮电出版社. 2014年6月第1版. 
\bibitem{tm-fawcett} Tom Fawcett. \emph{An introduction to ROC analysis}. Pattern Recognition Letters, 27(2006) 861-874. 
\bibitem{barsky} Marina Barsky. \url{http://csci.viu.ca/~barskym/}

\end{thebibliography}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%





